<template>
    <div>
        <el-card>
            <el-button type="primary" @click="dialogFormVisible = true">添加</el-button>

            <el-dialog title="添加" :visible.sync="dialogFormVisible">
                <el-form :model="insert">
                    <el-form-item label="角色" :label-width="'120px'">
                        <el-input style="width:100px;" size="medium" v-model="insert.role"
                                  autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="权限" :label-width="'120px'">
                        <el-input style="width:150px;" size="medium" v-model="insert.permission"
                                  autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>

                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="permissionAdd">确 定</el-button>
                </div>
            </el-dialog>

            <el-table :data="permissions">
                <el-table-column label="id">
                    <template scope="scope">
                        <span>{{scope.row.id}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="角色">
                    <template scope="scope">
                        <el-input v-if="scope.row.status" size="small" v-model="temp.role" placeholder="请输入内容"
                        ></el-input>
                        <span v-else>{{scope.row.role}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="权限">
                    <template scope="scope">
                        <el-input v-if="scope.row.status" size="small" v-model="temp.permission" placeholder="请输入内容"
                        ></el-input>
                        <span v-else>{{scope.row.permission}}</span>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="120">
                    <template slot-scope="scope">
                        <el-button v-if="scope.row.status" @click="confirmEdit(scope.$index,scope.row)" type="text"
                                   size="small">确认
                        </el-button>
                        <el-button v-else type="text" size="small" @click="novelDelete(scope.row)">删除</el-button>
                        <el-button v-if="scope.row.status" type="text" size="small"
                                   @click="cancelEdit(scope.$index, scope.row)">取消编辑
                        </el-button>
                        <el-button type="text" size="small" @click="edit(scope.$index, scope.row)" v-else>编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "PermissionManager",
        data() {
            return {
                permissions: [],
                temp: [],
                insert: {},
                dialogTableVisible: false,
                dialogFormVisible: false,
            }
        },
        mounted: function () {
            this.init();
        },
        methods: {
            init() {
                this.loadPermission();
            },
            permissionAdd() {

                this.$http.post("/permission/insertPermission", this.insert).then((res) => {
                    if (res.data.code == 0) {
                        this.$msg.success(res.data.message)
                        this.dialogFormVisible = false
                        this.loadPermission();
                    }
                })
            },
            loadPermission() {
                this.$http.get("/permission/listPermission").then((res) => {
                    if (res.data.code == 0) {
                        res.data.data.forEach((x) => {
                                x.status = false;
                            }
                        )
                        this.permissions = res.data.data

                    }
                })
            },
            handleEdit(index, row) {
                console.log(index, row);
                row.status = false;
                this.$set(this.permissions, index, row)
            },
            novelDelete(row) {
                this.$http.post("/permission/deletePermission", row).then((res) => {
                    if (res.data.code == 0) {
                        this.$msg.success(res.data.message + '请刷新页面')
                        this.loadPermission();
                    } else {
                        this.$msg.fail(res.data.message)
                    }
                })
            },
            edit(index, row) {
                this.temp = JSON.parse(JSON.stringify(row))
                row.status = true;
                this.$set(this.permissions, index, row)
            },
            cancelEdit(index, row) {
                row.status = false;
                this.$set(this.permissions, index, row)
            },
            confirmEdit(index, row) {
                row = JSON.parse(JSON.stringify(this.temp))
                this.$set(this.permissions, index, JSON.parse(JSON.stringify(this.temp)))
                row.status = false;
                this.$http.post("/permission/updatePermission", this.temp).then((res) => {
                    if (res.data.code == 0) {
                        this.$msg.success("更新成功");
                        this.loadPermission();
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>